from PyQt5.QtWidgets import QMainWindow, QStatusBar
from PyQt5.QtCore import Qt

from .controls import ControlManager, ControlId
from .interactions import InteractionManager
from .thumbnail_sidebar import ThumbnailSidebar

class MainWindow(QMainWindow):
    """PDF编辑器主窗口"""
    
    def __init__(self, pdf_doc, parent=None):
        super().__init__(parent)
        self._pdf_doc = pdf_doc
        
        # 初始化控件管理器
        self.control_manager = ControlManager(self)
        self.control_manager._controls[ControlId.MAIN_WINDOW] = self
        
        # 初始化缩略栏组件
        self.thumbnail_sidebar = ThumbnailSidebar()
        
        # 处理QSplitter
        splitter = self.control_manager.get(ControlId.SPLITTER)
        # 清除现有部件
        while splitter.count() > 0:
            widget = splitter.widget(0)
            widget.setParent(None)
        
        # 添加正确的子部件
        splitter.addWidget(self.thumbnail_sidebar)
        splitter.addWidget(self.control_manager.get(ControlId.VIEWER_CONTAINER))
        splitter.setSizes([150, 1050])
        
        # 设置中心部件
        self.setCentralWidget(self.control_manager.get(ControlId.CENTRAL_WIDGET))
        
        # 设置菜单栏和工具栏
        self.setMenuBar(self.control_manager.get(ControlId.MENU_BAR))
        self.addToolBar(self.control_manager.get(ControlId.TOOL_BAR))
        
        # 初始化交互管理器
        self.interaction_manager = InteractionManager(
            self.control_manager, 
            self._pdf_doc, 
            self,
            self.thumbnail_sidebar  # 传递缩略栏实例
        )
        
        # 窗口基本设置
        self.setWindowTitle("妖米PDF编辑器")
        self.setGeometry(100, 100, 1200, 800)
        self.setMinimumSize(800, 600)
        
        # 设置状态栏
        self.setStatusBar(QStatusBar())
        self.statusBar().showMessage("妖米PDF编辑器已就绪")
    
    def get_control(self, control_id):
        """通过ID获取控件"""
        return self.control_manager.get(control_id)
